<?php
/**
 * Created by PhpStorm.
 * User: lhuan
 * Date: 2019/11/4
 * Time: 22:05
 */
namespace app\index\model;

use think\Model;

class Article extends Model
{
    public $map = [];
    public $fields = '*';
    public $p = 1;
    public $limit = 8;

    public function initialize()
    {
        parent::initialize();

        $this->p = (int)input('param.p',1);
    }

    public function getArticle()
    {
        return $this->field($this->fields)->where($this->map)->find();
    }

    public function getList()
    {
        $data = $this->field($this->fields)->where($this->map)->order('sort desc,open_time desc')
            ->page($this->p,$this->limit)->select();

        $data = data_trans($data);

        foreach($data as $k => $v){
            $data[$k]['url'] = ArticleClassify::getUrl($v['pid'],$v['id']);
            $data[$k]['parent'] = ArticleClassify::getClassify($v['pid']);

            if(empty($v['picture'])){
                $data[$k]['picture'] = $this->getImg($v);
            }
        }

        return $data;
    }

    public function getAll()
    {
        $data = $this->field($this->fields)->where($this->map)->order('sort desc,open_time desc')->select();

        $data = data_trans($data);

        foreach($data as $k => $v){
            $data[$k]['url'] = ArticleClassify::getUrl($v['pid'],$v['id']);
            $data[$k]['parent'] = ArticleClassify::getClassify($v['pid']);

//            if(empty($v['picture'])){
//                $data[$k]['picture'] = $this->getImg($v);
//            }
        }

        return $data;
    }

    // side
    public function getFirstList($map,$order = 'sort desc,open_time desc')
    {
        $data = $this->field('*')->where($map)->orderRaw($order)
            ->page(1,5)->select();

        $data = data_trans($data);

        foreach($data as $k => $v){
            $data[$k]['url'] = ArticleClassify::getUrl($v['pid'],$v['id']);

            if(empty($v['picture'])){
                $data[$k]['picture'] = $this->getImg($v);
            }
        }

        return $data;
    }

    public static function getSide()
    {
        $model = new Article();
        $swiper = $model->getFirstList(
            [
                'status' => 1,
                'pid' => ['not in',0]
            ]
        );
        $hot = $model->getFirstList(
            [
                'status' => 1,
                'pid' => ['not in',0]
            ],
            'view_number desc'
        );
        return [
            'swiper' => $swiper,
            'hot' => $hot
        ];
    }

    public static function getPrev($id,$pid)
    {
        $time = date('Y-m-d H:m:s');
        $r = Article::where(['id' => ['lt',$id],'pid'=> $pid,'status'=>1,'open_time' => ['lt',$time]])->order('open_time desc')->find();
        if (!empty($r)){
            $r = data_trans($r);
            $r['url'] = ArticleClassify::getUrl($r['pid'],$r['id']);
            return $r;
        }else
            return false;
    }

    public static function getNext($id,$pid)
    {
        $time = date('Y-m-d H:m:s');
        $r = Article::where(['id' => ['gt',$id],'pid'=> $pid,'status'=>1,'open_time' => ['lt',$time]])->order('open_time desc')->find();
        if (!empty($r)){
            $r = data_trans($r);
            $r['url'] = ArticleClassify::getUrl($r['pid'],$r['id']);
            return $r;
        }else
            return false;
    }

    public function count()
    {
        return $this->where($this->map)->count();
    }

    private function getImg($v)
    {
        $reg = "/<img (.*?) src=\"(.+?)\".*?>/";
        $default = '/static/images/1564816736.jpg';

        preg_match_all($reg,$v['text'],$img);

        if(!empty($img[1])) {
            return $img[1][0];
        }else{
            return $default;
        }
    }

    public static function add($data)
    {
        $date = date('Y-m-d H:m:s');
        $data['create_time'] = $data['update_time'] = $date;
        return Article::insertGetId($data);
    }

    public static function modify($data)
    {
        $date = date('Y-m-d H:m:s');
        $data['update_time'] = $date;
        return Article::update($data);
    }

    public static function remove($id)
    {
        return Article::update([
            'id' => $id,
            'update_time' => date('Y-m-d H:m:s'),
            'status' => 0
        ]);
    }
}